package com.atguigu.lease.admin.controller.apartment;

import com.atguigu.lease.admin.service.RoomInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.RoomInfo;
import com.atguigu.lease.model.vo.apartment.RoomDetailVo;
import com.atguigu.lease.model.vo.apartment.RoomItemVo;
import com.atguigu.lease.model.vo.apartment.RoomQueryVo;
import com.atguigu.lease.model.vo.apartment.RoomSubmitVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "房间信息管理模块")
@RestController
@RequestMapping("/admin/room")
public class RoomController {
    @Autowired
    RoomInfoService roomInfoService;

    @Operation(summary = "1,新增或更新房间")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
        roomInfoService.saveOrUpdateRoomSubmitVo(roomSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "2,带条件分页查询房间列表")
    @GetMapping("/pageItem")
    public Result<Page<RoomItemVo>> pageItem(@RequestParam Long current,
                                             @RequestParam Long size,
                                             RoomQueryVo queryVo) {
        Page<RoomItemVo> page = roomInfoService.getPageItem(current, size, queryVo);
        return Result.ok(page);
    }

    @Operation(summary = "3、根据id查询房间详情")
    @GetMapping("/getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam(value = "id", required = true) Long id) {
        return Result.ok(roomInfoService.getDetailById(id));
    }

    ///admin/room/removeById?id=12
    @Operation(summary = "4,删除公寓")
    @DeleteMapping("/removeById")
    public Result removeById(@RequestParam Long id) {
        roomInfoService.deleteRoomInfoById(id);
        return Result.ok();
    }

    ///admin/room/updateReleaseStatusById
    @Operation(summary = "5、更新房间发布状态")
    @PostMapping("/updateReleaseStatusById")
    public Result updateReleaseStatusById(@RequestParam Long id,
                                          @RequestParam String status) {
        roomInfoService.update(Wrappers.lambdaUpdate(RoomInfo.class)
                .eq(RoomInfo::getId, id)
                .set(RoomInfo::getIsRelease, status));
        return Result.ok();
    }

    ///admin/room/listBasicByApartmentId?id=10
    @Operation(summary = "6、根据公寓id查询可用的房间列表")
    @GetMapping("/listBasicByApartmentId")
    public Result listBasicByApartmentId(@RequestParam("id") Long apartmentId) {
        // 查询房间表 + 租约表，查询可用的房间列表
        List<RoomInfo> roomInfos = roomInfoService.listBasicByApartmentId(apartmentId);
        return Result.ok(roomInfos);
    }
}